const express = require('express')
const router = express.Router()

const {
    getCustomerLists,
    pageGetCustomerLists,
    getCustomerNameLists,
    pageGetCustomerNameLists,
    deleteCustomer,
    batchDeleteCustomer,
    addCustomer,
    editCustomer,
} = require('../db/model/customerModel/customerModel')

/**
 * 查询所有顾客信息
 * get请求
 * 无参数
 */
router.get('/getCustomerLists',(req,res) => {
    const result = getCustomerLists()
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',customerLists:val})
        }else{
            res.send({code:-1,msg:'查询失败'})
        }
    })
})

/**
 * 分页查询所有顾客信息
 * post请求
 * 参数：page，pageSize
 */
router.post('/pageGetCustomerLists',(req,res) => {
    const { page,pageSize } = req.body
    const result = pageGetCustomerLists(page,pageSize)
    const result1 = getCustomerLists()
    Promise.all([result,result1]).then(val => {
        if(val.length!==0){
            res.send({code:200,msg:'查询成功',customerLists:val[0],count:val[1].length})
        }else{
            res.send({code:-1,msg:'查询失败',customerLists:val[0],count:val[1].length})
        }
    })
})

/**
 * 分页模糊查询顾客信息
 * post请求
 * 参数：name，page，pageSize
 */
router.post('/pageGetCustomerNameLists',(req,res) => {
    const { customerName,page,pageSize } = req.body
    const result = pageGetCustomerNameLists(customerName,page,pageSize)
    const result1 = getCustomerNameLists(customerName)
    Promise.all([result,result1]).then(val => {
        if(val.length!==0){
            res.send({code:200,msg:'查询成功',customerNameLists:val[0],count:val[1].length})
        }else{
            res.send({code:-1,msg:'查询失败',customerNameLists:val[0],count:val[1].length})
        }
    })
})

/**
 * 删除顾客
 * post请求
 * 参数：顾客id
 * 
 */
router.post('/deleteCustomer',(req,res) => {
    let { id } = req.body
    const result = deleteCustomer(id)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'删除成功'})
        }else{
            res.send({code:-1,msg:'删除失败'})
        }
    })
})

/** 
 * 批量删除顾客
 * post请求
 * 参数：数组顾客id
*/
router.post('/batchDeleteCustomer',(req,res) => {
    const { idAry } = req.body
    const result = batchDeleteCustomer(idAry)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'删除成功'})
        }else{
            res.send({code:-1,msg:'删除失败'})
        }
    })
})

/**
 * 新增顾客
 * post请求
 * 参数：name,age,gender,phone,consumption,level,regTime
 */
router.post('/addCustomer',(req,res) => {
    const { name,age,gender,phone,consumption,level,regTime } = req.body
    const result = addCustomer(name,age,gender,phone,consumption,level,regTime)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'添加成功'})
        }else{
            res.send({code:-1,msg:'添加失败'})
        }
    })
})

/**
 * 修改顾客信息
 * post请求
 * 参数：id,name,age,gender,phone,consumption,level,regTime
 */
router.post('/editCustomer',(req,res) => {
    const { id,name,age,gender,phone,consumption,level,regTime } = req.body
    const result = editCustomer(id,name,age,gender,phone,consumption,level,regTime)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'修改成功'})
        }else{
            res.send({code:-1,msg:'修改失败'})
        }
    })
})

module.exports = router